package org.itboys.mobile.service.mongo.web;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayProductRelated;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/8/15
 * 描述：
 */
@Service
public class SundayProductRelatedService extends BaseMongoService<SundayProductRelated>{
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayProductRelated> getEntityClass() {
        return SundayProductRelated.class;
    }

    @Autowired
    private SundayNewsService newsService;

    public PageResult<SundayProductRelated> select(HttpServletRequest request, Long productId){
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request, "name");
        Map<String,Object> param = new HashMap<>();
        param.put("isDeleted", 0);
        if(LongUtil.isNotNull(productId)){
            param.put("productId",productId);
        }
        PageResult<SundayProductRelated> result=super.containpageQuery(request,containsparam,param);
        return result;
    }
    public SundayProductRelated findOne(Long id){
        SundayProductRelated houseType=null;
        if(LongUtil.isNotZreo(id)){
            houseType=super.getById(id);
        }else {
            houseType=new SundayProductRelated();
        }
        return houseType;
    }
    public void save(Long productId,Long otherId,String otherName,String otherImage,Integer type){

        Map<String,Object> param=new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("productId",productId);
        param.put("otherId",otherId);
        param.put("type",type);
        List<SundayProductRelated> relateds = super.list(param);
        if(ListUtil.isNotNull(relateds)){
            throw new ErrorException("已存在相关内容");
        }else {
            SundayProductRelated related=new SundayProductRelated();
            related.setOtherId(otherId);
            related.setOtherName(otherName);
            related.setProductId(productId);
            related.setType(type);
            related.setOtherImage(otherImage);
            super.save(related);
        }
    }

}
